package com.shisfish.library.Controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.NewsImpl;
import com.shisfish.library.Impl.UserInterestImpl;
import com.shisfish.library.Model.News;
import com.shisfish.library.Model.UserInterest;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("news")
public class NewsController extends GlobalVariable {

	private NewsImpl newsImpl = new NewsImpl();
	private UserInterestImpl userInterestImpl = new UserInterestImpl();
	/**
	 * @author shisfish
	 * @date 2016年2月22日 下午4:57:54
	 * @Description 新增咨询
	 * @param idadmin
	 * @param title
	 * @param introduction
	 * @param author
	 * @param type
	 * @param context
	 * @param request
	 * @return
	 */
	@POST
	@Path("insertANews/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertANews(@PathParam("idadmin") String idadmin, @FormParam("title") String title, 
			@FormParam("introduction") String introduction, @FormParam("author") String author, 
			@FormParam("type") int type, @FormParam("context") String context, 
			@Context HttpServletRequest request, @FormParam("inter") int inter, 
			@FormParam("pic") String pic) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			News news = new News(title, introduction, author, type, context, inter, pic);
			int flag = newsImpl.insertOne(news);
			
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateANews/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateANews(@PathParam("idadmin") String idadmin, @FormParam("title") String title, 
			@FormParam("introduction") String introduction, @FormParam("author") String author, 
			@FormParam("type") int type, @FormParam("context") String context, 
			@Context HttpServletRequest request, @PathParam("id") int id, 
			@FormParam("inter") int inter, @FormParam("pic") String pic) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<News> newsList = newsImpl.selectById(id);
			
			if (newsList.size() > 0) {
				News news = newsList.get(0);
				news.setAuthor(author);
				news.setContext(context);
				news.setIntroduction(introduction);
				news.setTitle(title);
				news.setType(type);
				news.setInter(inter);
				news.setPic(pic);
				
				int flag = newsImpl.updateOne(news);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, INSERT_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("deleteANews/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String deleteANews(@PathParam("idadmin") String idadmin,  
			@Context HttpServletRequest request, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int flag = newsImpl.deleteOneById(id);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByPage/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByPage(@PathParam("type") int type, @PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = newsImpl.countByType(type);
		
		if (count > 0) {
			
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<News> newsList = newsImpl.selectByTypePage(type, page.getBeginIndex(), pageSize);
			
			if (newsList.size() > 0) {
				globalMap.put("list", newsList);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else if (newsList.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("selectByCho/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByCho(@FormParam("choose") String choose, @FormParam("type") int type, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize, 
			@FormParam("interest") String interest) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		//int count = newsImpl.countByCho(choose);
		int count = newsImpl.countCho(choose, type, interest);
		if (count > 0) {
			
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			//List<News> newsList = newsImpl.selectByChoPage(choose, page.getBeginIndex(), pageSize);
			List<News> newsList = newsImpl.selectChoPage(choose, type, interest, page.getBeginIndex(), pageSize);
			
			if (newsList.size() > 0) {
				globalMap.put("list", newsList);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else if (newsList.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectById/{id}")
	@Produces("application/json; charset=utf-8")
	public String selectById(@PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		News news = newsImpl.selectId(id);
		
		if (news != null) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("obj", news);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByPageE/{employee}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByPageE(@PathParam("employee") String employee, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<UserInterest> userInterests = userInterestImpl.selectByKey(employee);
		
		if (userInterests.size() > 0) {
			String interestStr = userInterests.get(0).getInterests();
			List<String> interestList = Arrays.asList(interestStr.split(","));
			int count = newsImpl.countAllE(interestList);
			
			if (count > 0) {
				
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<News> newsList = newsImpl.selectAllPageE(interestList, page.getBeginIndex(), pageSize);
				
				if (newsList.size() > 0) {
					globalMap.put("list", newsList);
					globalMap.put("page", page);
					globalMap.put(STATUS_CODE, SUCCESS);
				} else if (newsList.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
				
			} else if (count == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("selectByChoE/{employee}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByChoE(@FormParam("choose") String choose, @PathParam("employee") String employee, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<UserInterest> userInterests = userInterestImpl.selectByKey(employee);
		
		if (userInterests.size() > 0) {
			String interestStr = userInterests.get(0).getInterests();
			List<String> interestList = Arrays.asList(interestStr.split(","));
			int count = newsImpl.countByChoE(choose, interestList);
			
			if (count > 0) {
				
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<News> newsList = newsImpl.selectByChoPageE(choose, interestList, page.getBeginIndex(), pageSize);
				
				if (newsList.size() > 0) {
					globalMap.put("list", newsList);
					globalMap.put("page", page);
					globalMap.put(STATUS_CODE, SUCCESS);
				} else if (newsList.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
				
			} else if (count == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByPageInter/{interest}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectByPageInter(@PathParam("interest") int interest, @PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = newsImpl.countByInter(interest);
		
		if (count > 0) {
			
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<News> newsList = newsImpl.selectByInterPage(interest, currentPage, pageSize);
			
			if (newsList.size() > 0) {
				globalMap.put("list", newsList);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			} else if (newsList.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
}
